package com.cxg.fairy.ec.main.personal.message;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.personal.list.ListBean;
import com.cxg.fairy.ec.main.personal.list.ListItemType;
import com.cxg.fairy.ec.main.personal.list.MsgListAdapter;
import com.cxg.fairy.ec.main.search.SearchListAdapter;
import com.cxg.fairy.ec.main.search.SearchListDataConverter;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.MyDividerItemDecoration;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class MessageCenterDelegate extends LatteDelegate implements ISuccess {

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @BindView(R2.id.rv_settings)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_msg_center;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

        final ListBean funds = new ListBean.Builder()
                .setItemType(ListItemType.ITEM_IMG)
                .setId(1)
                .setDelegate(new MessageCenterFundsDelegate())
                .setText(getResources().getString(R.string.msg_center_my_funds))
                .build();

        final ListBean orderNoti = new ListBean.Builder()
                .setItemType(ListItemType.ITEM_IMG)
                .setId(2)
                .setDelegate(new MessageCenterFundsDelegate())
                .setText(getResources().getString(R.string.msg_center_order_notice))
                .build();

        final ListBean promote = new ListBean.Builder()
                .setItemType(ListItemType.ITEM_IMG)
                .setId(3)
                .setDelegate(new MessageCenterFundsDelegate())
                .setText(getResources().getString(R.string.msg_center_promote))
                .build();

        final ListBean osNoti = new ListBean.Builder()
                .setItemType(ListItemType.ITEM_IMG)
                .setId(4)
                .setDelegate(new MessageCenterFundsDelegate())
                .setText(getResources().getString(R.string.msg_center_sys_notice))
                .build();

        final List<ListBean> data = new ArrayList<>();
        data.add(funds);
        data.add(orderNoti);
        data.add(promote);
        data.add(osNoti);

    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.builder()
                .url("mc/getMcCate")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @Override
    public void onSuccess(String response) {
        final ArrayList<MultipleItemEntity> data = new MessageCenterListDataConverter().setJsonData(response).convert();
        //设置RecyclerView
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);
        MyDividerItemDecoration itemDecoration = new MyDividerItemDecoration(this.getContext());
        mRecyclerView.addItemDecoration(itemDecoration);
        final MsgListAdapter adapter = new MsgListAdapter(data);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnItemTouchListener(new MessageCenterClickListener(this));
    }
}